package com.mvvm.library.base;

import androidx.lifecycle.ViewModel;

import com.mvvm.library.http.RetrofitService;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @创建时间：2023/2/22
 * @作者：小蔡
 * @功能介绍：
 */
public abstract class BaseModel extends ViewModel{
    protected RetrofitService retrofitService = RetrofitService.getInstance();
    private CompositeDisposable compositeDisposable;
    @SuppressWarnings("unchecked")
    protected void addDisposable(Observable<?> observable, BaseObserver observer) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable
                .add(observable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeWith(observer));
    }
    private void removeDisposable() {
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
        }
    }
}
